package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.SaUserCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dao.entity.UserDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.AIReviewReadDTO;
import com.cskaoyan.wordmemorize.dto.app.UserFavoriteDTO;
import com.cskaoyan.wordmemorize.dto.app.UserVocBookStatisticsDTO;
import com.cskaoyan.wordmemorize.request.AIReviewReadCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserFavoriteCommand;
import com.cskaoyan.wordmemorize.service.UserPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/priviledge")
public class AppUserPrivilegeController {


    @Autowired
    UserPrivilegeService userPrivilegeService;

    @Autowired
    UserMapper userMapper;


    @GetMapping("/current/{bookId}/statistics")
    @SaUserCheckPermission("current:book")
    public Result<UserVocBookStatisticsDTO> getSelectedBookStatistics(@PathVariable Long bookId) {

        Long userId = StpKit.USER.getLoginIdAsLong();

        /*// 二次验证等级（可选，增加安全性）
        UserDO user = userMapper.selectById(userId);
        if (user.getLevelValue() < 2) {
            throw new BusinessException(ResultCodeEnum.PERMISSION);
        }*/
        UserVocBookStatisticsDTO selectedBookStatistics = userPrivilegeService.getSelectedBookStatistics(userId, bookId);
        return Result.ok(selectedBookStatistics);
    }

    @PostMapping("/voc/non")
    public Result notRemember(@RequestParam Long vocId) {

        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.notRemember(vocId, userId);

        return Result.ok();
    }

    @PutMapping("/voc/non/cancel")
    public Result cancelNotRemember(@RequestParam Long vocId) {

        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.cancelNotRemember(vocId, userId);

        return Result.ok();
    }


    @PostMapping("/favorite")
    public Result addFavorite(@RequestBody UserFavoriteCommand command) {

        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.addFavorite(command, userId);

        return Result.ok();
    }

    @PostMapping("/favorite/bulk")
    public Result addFavoriteBulk(@RequestBody List<UserFavoriteCommand> command) {

        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.addFavoriteBulk(command, userId);

        return Result.ok();
    }

    @PostMapping("/favorite/cancel")
    public Result cancelFavorite(@RequestBody List<Long> ids) {

        Long userId = StpKit.USER.getLoginIdAsLong();
        userPrivilegeService.cancelFavorite(ids, userId);

        return Result.ok();
    }

    @GetMapping("/favorite/list")
    public Result<PageDTO<UserFavoriteDTO>> getUserFavoriteList(PageRequest pageRequest) {

        Long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<UserFavoriteDTO> userFavoriteList = userPrivilegeService.getUserFavoriteList(pageRequest, userId);
        return Result.ok(userFavoriteList);
    }


}
